<?php

namespace app\home\controller;

use think\Controller;
use think\Request;

class Goods extends Base
{
    public function list()
	{
		$id=input('id');
		$list=\app\common\model\Goods::where('cate_id',$id)->paginate(10);
		$categorynav=\app\common\model\Category::find($id);
		return view('list',['list'=>$list,'categorynav'=>$categorynav]);
	}
	
	public function detail()
	{
			$id=input('id');
			$goodsinfo=\app\common\model\Goods::find($id);
			$goodsinfo['goods_attr']=json_decode($goodsinfo['goods_attr'],true);
			$goodsimages=\app\common\model\GoodsImages::where('goods_id',$id)->select();
			//dump($goodsinfo);
			$goodsspec=\app\common\model\SpecGoods::where('goods_id',$id)->select();
			
			//映射表value_ids
			$value_ids_map=[];
			foreach($goodsspec as $v){
				$row=['id'=>$v['id'],'price'=>$v['price']];
				$value_ids_map[$v['value_ids']]=$row;
			}
			//dump($value_ids_map);
			$value_ids_map=json_encode($value_ids_map);
			
			$val=array_column($goodsspec->toArray(),"value_ids");
			//dump($val);
			
			//   去重                          分开                        连起来
			$value_ids=array_unique((explode('_',implode('_',$val))));
			$spec_info=\app\common\model\SpecValue::with('Spec')->where('id','in',$value_ids)->select();
	//		dump($spec_info);
			
			$res=[];
			foreach($spec_info as $v){
				$res[$v['spec_id']]=[
					'spec_id'=>$v['spec_id'],
					'spec_name'=>$v['spec_name'],
					'spec_values'=>[]
				];
			}
			
			foreach($spec_info as $v){
				$res[$v['spec_id']]['spec_values'][]=$v;
			}
			
			return view('detail',['goodsinfo'=>$goodsinfo,'goodsimages'=>$goodsimages,'res'=>$res,'map'=>$value_ids_map]);
	}
}
